package book.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import book.dao.BaseDao;
import book.dao.RecordDao;
import book.util.Record;

public class RecordDaoImpl implements RecordDao{
	private Connection connection;
	private ResultSet rs;
	private PreparedStatement pstmt;
	public int insertfrom(Record record) {
		int num=0;
			try {
		connection=BaseDao.getConnection();
		String sql="insert into record(book_ID,card_ID,borrow_data,return_data,manager_ID )values(?,?,?,?,?,?)";
		pstmt=connection.prepareStatement(sql);
		pstmt.setInt(1, record.getBook_ID());
		pstmt.setInt(2, record.getCard_ID());
		pstmt.setString(3,record.getBorrow_data());
		pstmt.setTimestamp(4,new Timestamp(System.currentTimeMillis()));
		pstmt.setInt(5,record.getManager_ID());
		//Timestamp borrow_data=rs.getTimestamp("borrow_data");
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
			
			return num;
	}

	

	public int deletefrom(int Book_id) {
		int num=0;
		try {
		connection=BaseDao.getConnection();
		String sql="delete from record where Book_id=?";
		
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, Book_id);
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}

	public int updateset(Record record) {
		int num=0;
		try {
		connection=BaseDao.getConnection();
		String sql="update record set card_ID=?,borrow_data=?,return_data=?,manager_ID=? where book_ID=?";
		pstmt.setInt(5, record.getBook_ID());
		pstmt.setInt(1, record.getCard_ID());
		pstmt.setString(2,record.getBorrow_data());
		pstmt.setString(3,record.getReturn_data());
		
			pstmt.setInt(4,record.getManager_ID());
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}

	public List<Record> findRecordAll() {
		List<Record> rdlist=new ArrayList<Record>();
		try {
		connection=BaseDao.getConnection();
		String sql="select book_ID,card_ID,borrow_data,return_data,manager_ID from record";
		pstmt=connection.prepareStatement(sql);
		
			rs=pstmt.executeQuery();
			while(rs.next()){
				Record a=new Record(rs.getInt("book_ID"),rs.getInt("card_ID"),
						rs.getString("borrow_data"),rs.getString("return_data"),
						rs.getInt("manager_ID"));
				rdlist.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		
		return rdlist;
	}

}
